package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.config.ThemeConfig;
import com.uchengwang.functions.http.config.UUConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.ucity.adapter.ApplyAdapter;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UserBeanList;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/6/15.
 * 享说所有点赞用户
 */

public class ZanPersonsActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private LinearLayoutManager layoutManager;
    private ApplyAdapter adapter;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private int page = 1;//当前页  每页请求多少数据
    private long id;
    private int type ;//请求类别 1享说 2兴趣圈话题评论 3U城话题
    private int totlaPagers ;//总页数【服务器返回】

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        id = getIntent().getLongExtra(BaseConfig.ID, -1);
        type = getIntent().getIntExtra(BaseConfig.TYPE, -1);

        setTitle("点赞详情");
        initView();

    }

    private void initView() {
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);

        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);

        initData();

    }

    private void initData() {
        layoutManager = new LinearLayoutManager(this);
        adapter = new ApplyAdapter(this, false);
        recycleView.setLayoutManager(layoutManager);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                initHttpData(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    initHttpData(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });

    }

    /**
     * @param isRefresh 获取点赞用户数据
     * */
    private void initHttpData(final boolean isRefresh){
        MapRequest map = new MapRequest();
        IHttpManager.RequestMethod method = null;
        String url = "";
        switch (type){
            case 1://享说点赞
                method = UUConfig.SpeekZan.method_get;
                url = UUConfig.SpeekZan.URL;
                map.put(BaseConfig.SHARE_ID , id);
                break;
            case 2://话题点赞
                method = ClubConfig.TopicZan.method_get;
                url = ClubConfig.TopicZan.URL;
                map.put(BaseConfig.CMT_ID , id);
                break;
            case 3://U城话题
                method = ThemeConfig.ThemeLike.method_get;
                url = ThemeConfig.ThemeLike.URL;
                map.put(BaseConfig.SHARE_ID , id);
                break;
        }
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 20);
        HttpManager.newInstance(this).query(method, url, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                UCommonUtil.getUtil().showToast(ZanPersonsActivity.this, "请求失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                if (status != 0){
                    UCommonUtil.getUtil().showToast(ZanPersonsActivity.this, msg);
                    return;
                }
                if (!TextUtils.isEmpty(data)){
                    UserBeanList bean = JsonUtils.getManager().stringToBean(data, UserBeanList.class);
                    if (bean != null){
                        List<UserBean> beanList = bean.getData();
                        totlaPagers = bean.getTotalPage();
                        if (isRefresh){
                            adapter.setData(beanList);
                        }else {
                            adapter.getData().addAll(beanList);
                            adapter.notifyDataSetChanged();
                        }

                    }
                }
            }
        });

    }

}
